package com.eco.schedule;


import com.eco.constants.Constants;
import com.eco.netty.NettyClientConfig;
import com.eco.utils.KndUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledFuture;

@Slf4j
@Component
@EnableScheduling
public class LightScheduleTask implements SchedulingConfigurer {

    @Value("${lamplight.start:}")
    private String onCron;

    @Value("${lamplight.end:}")
    private String offCron;

    private final NettyClientConfig nettyClientConfig;

    private final StringRedisTemplate stringRedisTemplate;

    private ScheduledFuture<?> onFuture;

    private ScheduledFuture<?> offFuture;

    public LightScheduleTask(StringRedisTemplate stringRedisTemplate, NettyClientConfig nettyClientConfig) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.nettyClientConfig = nettyClientConfig;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);

        onFuture = taskScheduler.schedule(() -> {
            log.info("执行开启灯带定时任务...");
            //同时控制两个灯带
            KndUtil.sendCommand(nettyClientConfig.getClient1().getClientId(),Constants.KND_TURN_ON);
            KndUtil.sendCommand(nettyClientConfig.getClient2().getClientId(),Constants.KND_TURN_ON);
            stringRedisTemplate.opsForValue().set(Constants.SENSOR_REDIS_KEY,Constants.SENSOR_ON);
            String lambStatus = stringRedisTemplate.opsForValue().get(Constants.SENSOR_REDIS_KEY);
            log.info("lambStatus:{}",lambStatus);
        }, triggerContext -> {
            CronTrigger trigger = new CronTrigger(onCron);
            return trigger.nextExecutionTime(triggerContext);
        });

        offFuture = taskScheduler.schedule(() -> {
            log.info("执行关闭灯带定时任务...");
            //同时控制两个灯带
            KndUtil.sendCommand(nettyClientConfig.getClient1().getClientId(),Constants.KND_TURN_OFF);
            KndUtil.sendCommand(nettyClientConfig.getClient2().getClientId(),Constants.KND_TURN_OFF);
            stringRedisTemplate.opsForValue().set(Constants.SENSOR_REDIS_KEY,Constants.SENSOR_OFF);
            String lambStatus = stringRedisTemplate.opsForValue().get(Constants.SENSOR_REDIS_KEY);
            log.info("lambStatus:{}",lambStatus);
        }, triggerContext -> {
            CronTrigger trigger = new CronTrigger(offCron);
            return trigger.nextExecutionTime(triggerContext);

        });
    }


    /**
     * 修改定时设置
     * @param startCron
     * @param stopCron
     */
    public void updateCronExpression(String startCron,String stopCron) {
        this.onCron = startCron;
        this.offCron = stopCron;
        log.info("startCron:{}",startCron);
        log.info("stopCron:{}",stopCron);
        if (onFuture != null) {
            onFuture.cancel(true);
        }
        if(offFuture != null) {
            offFuture.cancel(true);
        }
        configureTasks(new ScheduledTaskRegistrar());
    }


}
